import ros.*;
import ros.communication.*;
import processing.core.PApplet;

import java.util.Vector;
import java.util.Iterator;

import ros.pkg.word_draw_processing.msg.*;

class WordObject{
    public String word;
    public String category;
    public int id;
    public int link;
    public int[] spos;
    public int[] epos;

    public WordObject(){
	spos = new int[2];
	epos = new int[2];
    }
}


class DrawPApplet extends PApplet{
    private int width=600;//描画部の横サイズ
    private int height=300;//描画部の縦サイズ
    private int fontSize = 12;//フォントのサイズ
    private int fontStrideCount = 5;//ワードとワードのスペース数

    private Vector<WordObject> word_object_ = new Vector<WordObject>();

    public void inputData(WordDrawLayout inWdl){

	word_object_.clear();

	int line_count = 1;
	int font_stride = 0;
	for(int n=0; n<inWdl.wdd.size(); n++){
	    WordObject tmp_word_object_ = new WordObject();
	    WordDrawDimension tmp_word_drow_dim_ = inWdl.wdd.get(n);
	    tmp_word_object_.word = tmp_word_drow_dim_.word;
	    tmp_word_object_.category = tmp_word_drow_dim_.category;
	    tmp_word_object_.id = tmp_word_drow_dim_.id;
	    tmp_word_object_.link = tmp_word_drow_dim_.link;
	    tmp_word_object_.spos[0] = fontSize * fontStrideCount + font_stride;
	    tmp_word_object_.spos[1] = fontSize * 2 * line_count;
	    tmp_word_object_.epos[0] = tmp_word_object_.spos[0] + tmp_word_object_.word.length()*fontSize;
	    tmp_word_object_.epos[1] = fontSize * 2 * line_count;
	    word_object_.add(tmp_word_object_);

	    if(tmp_word_object_.id + 1 == tmp_word_object_.link ){
		font_stride = tmp_word_object_.epos[0];
	    } else {
		font_stride = 0;
		line_count++;
	    }

	}

    };


    public void setup(){
        size(width,height);

        textFont( createFont("Default", 9) );

        smooth();
        noLoop();

    }


    public void draw(){
	
        background(255,255,255);

        fill(0);

        textSize(fontSize);

        textAlign(LEFT);

	for(int n=0; n<word_object_.size(); n++){
	    WordObject tmp_word_object_ = word_object_.get(n);
	    text(tmp_word_object_.word, tmp_word_object_.spos[0], tmp_word_object_.spos[1]);
	}

	for(int n=0; n<word_object_.size(); n++){
	    WordObject tmp1_word_object_ = word_object_.get(n);

	    for(int m=0; m<word_object_.size(); m++){
		WordObject tmp2_word_object_ = word_object_.get(m);

		if(tmp1_word_object_.link == tmp2_word_object_.id){
		    noFill();
		    bezier(tmp1_word_object_.epos[0]+fontSize, tmp1_word_object_.epos[1]-fontSize/2,
			   tmp1_word_object_.epos[0]+100, tmp1_word_object_.epos[1]-fontSize/2,
			   tmp2_word_object_.spos[0]-100, tmp2_word_object_.spos[1]-fontSize/2,
			   tmp2_word_object_.spos[0]-fontSize, tmp2_word_object_.spos[1]-fontSize/2
			   );
		    break;
		}
	    }

	}
    }
}